//
//  XCFeedbackViewController.m
//  Diary_WhatsUrName?
//
//  Created by 蔡湘辉 on 2018/12/24.
//  Copyright © 2018 王冠宇. All rights reserved.
//

#import "XCFeedbackViewController.h"

@interface XCFeedbackViewController ()<QMUITextViewDelegate>

@property (nonatomic, strong) QMUITextView *textView;
@property (nonatomic, strong) UILabel *tipsLabel;
@property (nonatomic, strong) QMUIFillButton *sendBtn;

@end

@implementation XCFeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"意见反馈";
    [self textView];
    [self tipsLabel];
    [self sendBtn];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)sendBtnClick {
    __weak typeof(self) weakSelf = self;
    QMUITips *tips1 = [QMUITips showLoading:@"正在发送..." inView:self.view hideAfterDelay:1.0f];
    tips1.didHideBlock = ^(UIView *hideInView, BOOL animated) {
        QMUITips *tips2 = [QMUITips showSucceed:@"发送成功" inView:weakSelf.view hideAfterDelay:1.0f];
        tips2.didHideBlock = ^(UIView *hideInView, BOOL animated) {
            [weakSelf.navigationController popViewControllerAnimated:YES];
        };
    };
}

- (QMUITextView *)textView {
    if (_textView == nil) {
        _textView = [[QMUITextView alloc] init];
        [self.view addSubview:_textView];
        [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
            if (@available(iOS 11, *)) {
                make.top.mas_equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(20);
            } else {
                make.top.mas_equalTo(self.view).offset(20);
                make.bottom.mas_equalTo(self.view);
            }
            make.left.mas_equalTo(self.view).offset(20);
            make.right.mas_equalTo(self.view).offset(-20);
            make.height.mas_equalTo(140);
        }];
        _textView.delegate = self;
        _textView.placeholder = @"请输入您宝贵的意见和建议";
        _textView.placeholderColor = UIColorPlaceholder; // 自定义 placeholder 的颜色
        _textView.textContainerInset = UIEdgeInsetsMake(10, 7, 10, 7);
        _textView.returnKeyType = UIReturnKeySend;
        _textView.enablesReturnKeyAutomatically = YES;
        _textView.typingAttributes = @{NSFontAttributeName: UIFontMake(15),
                                           NSForegroundColorAttributeName: UIColorGray1,
                                           NSParagraphStyleAttributeName: [NSMutableParagraphStyle qmui_paragraphStyleWithLineHeight:20]};
        // 限制可输入的字符长度
        _textView.maximumTextLength = 100;
        
        // 限制输入框自增高的最大高度
        _textView.maximumHeight = 200;
        
        _textView.layer.borderWidth = PixelOne;
        _textView.layer.borderColor = UIColorSeparator.CGColor;
        _textView.layer.cornerRadius = 4;
    }
    return _textView;
}

- (UILabel *)tipsLabel {
    if (_tipsLabel == nil) {
        _tipsLabel = [UILabel new];
        [self.view addSubview:_tipsLabel];
        [_tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.textView.mas_bottom).offset(10);
            make.left.right.mas_equalTo(self.textView);
        }];
        _tipsLabel.attributedText = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"最长不超过 %@ 个文字，可尝试输入 emoji、粘贴一大段文字。", @(self.textView.maximumTextLength)] attributes:@{NSFontAttributeName: UIFontMake(12), NSForegroundColorAttributeName: UIColorGray6, NSParagraphStyleAttributeName: [NSMutableParagraphStyle qmui_paragraphStyleWithLineHeight:16]}];
        _tipsLabel.numberOfLines = 0;
    }
    return _tipsLabel;
}

- (QMUIFillButton *)sendBtn {
    if (_sendBtn == nil) {
        _sendBtn = [[QMUIFillButton alloc] initWithFillType:QMUIFillButtonColorBlue];
        _sendBtn.titleLabel.font = UIFontMake(14);
        [_sendBtn setTitle:@"发送" forState:UIControlStateNormal];
        [self.view addSubview:_sendBtn];
        [_sendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self.textView);
            make.top.mas_equalTo(self.tipsLabel.mas_bottom).offset(50);
            make.height.mas_equalTo(50);
        }];
        [_sendBtn addTarget:self action:@selector(sendBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sendBtn;
}

@end
